package cn.edu.nit.sys.mapper;

import cn.edu.nit.sys.entity.Forum;
import cn.edu.nit.sys.entity.ForumLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.Map;

/**
 * @Author 罗文斌
 * @Date 2023/4/11
 * @Version 1.0.0
 */
@Mapper
public interface ForumMapper extends BaseMapper<Forum> {

    Page<Forum> getApproveList(Page<Forum> page, String siftName, String siftStatus, String startTime, String endTime, String uid);

    @Insert("insert into sys_forum_g(id, forum_id, auditor_id, result, opinion, time) " +
            "values(#{params.id}, #{params.forumId}, #{params.author}, #{params.result}, #{params.opinion}, #{params.time})")
    int insertApproveLog(@Param("params") ForumLog forumLog);

    @Update("update sys_forum set status = #{status} where id = #{id}")
    int approveForum(String id, String status);

    @Select("select a.id, c.id as authorId, b.avatar, b.nickname as author, a.content, a.images, a.create_time, d.name as clazz, " +
            "a.views, a.likes, " +
            "if((select id from sys_forum_log where fid = #{id} and uid = #{uid} and type = 2) is not null, true, false) as liked " +
            "from sys_forum a " +
            "left join sys_user b on a.user_id = b.id " +
            "left join sys_student c on c.id = b.username " +
            "left join sys_department d on d.dept_id = c.dept_id and d.major_id = c.major_id " +
            "where a.id = #{id}")
    Forum getDetail(String id, String uid);

    @Select("select a.id, a.user_id as userId, b.nickname, a.content, a.time, b.avatar, a.ip " +
            "from sys_forum_c a " +
            "left join sys_user b on a.user_id = b.id " +
            "where a.forum_id = #{forumId} and a.logic_delete = '0'")
    @MapKey("id")
    Page<Map<String, String>> getCommentList(Page<Map<String, String>> page, String forumId);

    @Insert("insert into sys_forum_c(id, forum_id, user_id, content, ip, lng_lat, time) " +
            "values (#{id}, #{forumId}, #{userId}, #{comment}, #{ip}, #{lngLat}, now())")
    int addForumComment(String id, String forumId, String userId, String comment, String ip, String lngLat);

    @Update("update sys_forum_c set logic_delete = '1' where id = #{id}")
    int delComment(String id);

    @Update("update sys_forum_c set content = #{comment}, time = now() where id = #{id}")
    int updateComment(String id, String comment);

    @Update("update sys_forum set views = views + 1 where id = ${id}")
    void plusView(String id);

    @Select("select id from sys_forum_log where fid = #{id} and uid = #{uid} and type = 1")
    String getViewData(String id, String uid);

    @Insert("insert into sys_forum_log(id, fid, uid, type) values (#{uuid}, #{id}, #{uid}, #{type})")
    int insertForumLog(String uuid, String id, String uid, int type);

    @Select("select id from sys_forum_log where fid = #{fid} and uid = #{uid} and type = 2")
    String forumLikes(String fid, String uid);

    @Update("update sys_forum set likes = likes + 1 where id = #{fid}")
    int plusLikes(String fid);

    @Update("update sys_forum set likes = likes - 1 where id = #{fid}")
    int reduceLikes(String fid);

    @Delete("delete from sys_forum_log where fid = #{fid} and uid = #{uid} and type = #{type}")
    int removeLog(String fid, String uid, int type);

    @Select("select user_id from sys_forum where id = #{fid}")
    String getFormAuthor(String fid);
}
